﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel
{
    public partial class Account : Form
    {
        public Account()
        {
            InitializeComponent();
        }

        public static string UserName = string.Empty;
        CNDatabase dt = new CNDatabase();

        private void PgAccountBtCONFIRM_Click(object sender, EventArgs e)
        {
            //if (TbID.Text.Length < 4)
            //{
            //    //LbLoi1.Text = "ID must be at least 5 characters";
            //    LbLoi1.Visible = true;
            //}

            //else if (TbCode.Text.Length < 5)
            //{
            //    PgAccountLbLoi2.Text = "Password is too short";
            //    PgAccountLbLoi2.Visible = true;
            //}
            //else if (TbRetypeCode.Text != TbCode.Text)
            //    PgAccountLbLoi3.Visible = true;
            //else
            //{

            try
            {
                dt.connect();
                string queryInsert = "insert into Account(IDAccount, Fullname, Birthday, Address, Phone, Email, Password, IDRole) values(N'"
                    + TbID.Text + "', '" + TbName.Text + "', convert(datetime,'" + DatePickBirth.Text + "', 103), '"
                    + TbAddress.Text + "','" + TbPhone.Text + "', '" + TbMail.Text + "', '"
                    + TbCode.Text + "', '" + CbRole.SelectedValue + "')";
                SqlCommand cm = new SqlCommand(queryInsert, dt.cn);
                cm.ExecuteNonQuery();
                dt.cn.Close();
                MessageBox.Show("Added new account's information successfully !", "Information", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                //setClear();
                PgAccountGb1.Visible = false;
                //this.Size = new System.Drawing.Size(536, 502); 
                loadload();
                }
                catch (Exception)
                {
                    dt.cn.Close();
                    MessageBox.Show("Lỗi! Yêu cầu nhập lại");

                }


            }
      

        private void loadload() {
            dt.connect();
            string qr = "select * from Role";
            DataTable tb = new DataTable();
            SqlDataAdapter adt = new SqlDataAdapter(qr,dt.cn);
            adt.Fill(tb);
            CbRole.DataSource = tb;
            dt.cn.Close();
            CbRole.DisplayMember = "Type";
            CbRole.ValueMember = "IDRole";

            //dt.connect();

            DataTable ds = new DataTable();
            SqlDataAdapter dta = new SqlDataAdapter("SELECT * FROM Account", dt.cn);
            dta.Fill(ds);
            BindingSource bdS = new BindingSource();
            bdS.DataSource = ds;           
            dataGridView1.DataSource = bdS;
            bindingNavigator1.BindingSource = bdS;
            this.TbName.DataBindings.Add("Text", bdS, "Fullname");
            this.TbCode.DataBindings.Add("Text", bdS, "Password");
            this.TbPhone.DataBindings.Add("Text", bdS, "Phone");
            this.DatePickBirth.DataBindings.Add("Text", bdS, "Birthday");
            this.TbAddress.DataBindings.Add("Text", bdS, "Address");
            this.TbMail.DataBindings.Add("Text", bdS, "Email");
            this.TbID.DataBindings.Add("Text", bdS, "IDAccount");
            dt.cn.Close();
            //string qr = "select * from Account, Role where Account.IDRole=Role.IDRole";
            //BindingSource ds = dt.getData(qr);
            //dataGridView1.DataSource = ds;
            //bindingNavigator1.BindingSource = ds;

        }

        private void PgAccount_Load(object sender, EventArgs e)
        {
            loadload();
            //TbID.Text = UserName;


            //////
            //cb1.DataSource = tb;
            //cb1.DisplayMember = "ID";
            //////
            //cb2.DataSource = tb;
            //cb2.DisplayMember = "ID";
            ////



        }



        private void PgAccountTbID_TextChanged(object sender, EventArgs e)
        {
            if (TbID.Text.Length > 4)
            {
                LbLoi1.Visible = false;
                PgAccountLb4.Visible = false;
            }
        }
        private void PgAccountTbName_TextChanged(object sender, EventArgs e)
        {
            if (TbID.Text == "")
            {
                LbLoi1.Visible = true;
                LbLoi1.Text = "Entry Requirements";
                PgAccountLb4.Visible = true;
            }
            if (TbID.Text.Length < 4)
            {
                LbLoi1.Visible = true;
                PgAccountLb4.Visible = true;
                LbLoi1.Text = "ID must be at least 5 characters";

            }
            if (TbID.Text.Length > 4)
            {
                LbLoi1.Visible = false;
                PgAccountLb4.Visible = false;
            }
        }


        private void PgAccountTbRetypeCode_TextChanged(object sender, EventArgs e)
        {
            if (TbCode.Text == "")
            {
                PgAccountLbLoi2.Visible = true;
                PgAccountLbLoi2.Text = "Entry Requirements";
                PgAccountLb4.Visible = true;
            }
            if (TbCode.Text.Length < 5)
            {
                PgAccountLbLoi2.Visible = true;
                PgAccountLb4.Visible = true;
                PgAccountLbLoi2.Text = "At least 5 characters";

            }
            if (TbCode.Text.Length > 4)
            {
                PgAccountLbLoi2.Visible = false;
                PgAccountLb4.Visible = false;
            }
            //if (PgAccountTbRetypeCode.Text != PgAccountTbCode.Text)
            //{
            //    PgAccountLbLoi3.Visible = true;
            //    PgAccountLb4.Visible = true;
            //    PgAccountLbLoi3.Text = "Access Code does not match";
            //}
        }

        private void PgAccountTbCode_TextChanged(object sender, EventArgs e)
        {
            if (TbCode.Text.Length > 4)
            {
                PgAccountLbLoi2.Visible = false;
                PgAccountLb4.Visible = false;
            }
        }

        

       

      
    }
}

       


   


        

